package com.rdc.soulmatemusicplayer.util;

import android.content.Context;
import android.database.Cursor;

import android.provider.MediaStore;
import android.widget.Toast;

import com.rdc.soulmatemusicplayer.application.App;
import com.rdc.soulmatemusicplayer.bean.Song;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 对本地歌曲操作的工具类
 * @date 创建日期  2021-04-17 0:28
 */

public class MusicUtils
{
	//歌曲列表
	public static List<Song> sMusicList = new ArrayList<Song>();

	public static void initMusicList()
	{
		// 获取歌曲列表
		sMusicList.clear();
		sMusicList.addAll(getMusicData());
	}

	public static List<Song> getMusicData()
	{
		Toast.makeText(App.sContext, "搜索本地歌曲中，请稍等！", Toast.LENGTH_SHORT).show();
		//媒体库查询
		Cursor cursor = App.sContext.getContentResolver()
				.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);
		if (cursor != null)
		{
			while (cursor.moveToNext())
			{
				Song song = new Song();
				song.id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				song.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				song.artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
				song.uri = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				song.length = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
//								song.image = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
//
//								if (song.size > 1000 * 800)
//								{
//									// 注释部分是切割标题，分离出歌曲名和歌手 （本地媒体库读取的歌曲信息不规范）
//									if (song.title.contains("-"))
//									{
//										String[] str = song.title.split("-");
//										song.title = str[0];
//										song.artist = str[1];
//									}
//								}
				sMusicList.add(song);
			}
		}

		return sMusicList;
	}

	/**
	 * 定义一个方法用来格式化获取到的时间
	 */
	public static String formatTime(int time)
	{
		if (time / 1000 % 60 < 10)
		{
			return time / 1000 / 60 + ":0" + time / 1000 % 60;
		} else
		{
			return time / 1000 / 60 + ":" + time / 1000 % 60;
		}
	}

}